// 65465455656456
const basePage = require('../../../common/BasePage')
Page({
  behaviors: [basePage],

  /**
   * 页面的初始数据
   */
  data: {
    storeSid: '',
    /**用户sid */
    sid: '',
    itemArr: [],
    user: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.data.storeSid = options.storeSid
    if (options.sid) {
      this.request({
        url: this.data.host + '/v1/users/member/' + options.sid,
      }, res => {
        res.data.thumb = this.get_imgUrl(res.data.avatar);
        this.setData({
          user: res.data
        })
      })

    }
    this.setData({
      sid: options.sid || "",
      storeSid: options.storeSid || ""
    })

  },
  handleChange(e) {
    this.data.itemArr[e.currentTarget.dataset.index].goodsNum = e.detail.value
    this.setData({
      itemArr: this.data.itemArr
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  del_click(e) {
    this.data.itemArr.splice(e.currentTarget.dataset.index, 1);
    this.setData({
      itemArr: this.data.itemArr
    })
  },
  scan_bar() {
    wx.scanCode({
      onlyFromCamera: true,
      scanType: ["qrCode", "barCode"],
      success: (result) => {

        if (result.result) {
          this.request({
            url: this.data.host + '/v1/orders/' + result.result + '/member-scan/' + this.data.storeSid,

            method: "post",

          }, res => {
            res.data.thumb = this.get_imgUrl(res.data.image);
            res.data.goodsNum = 1;
            this.data.itemArr.push(res.data);
            this.setData({
              itemArr: this.data.itemArr
            })
          })
        }
      },
      fail: (res) => {
        console.log('scanCode fail', res);
        // wx.showToast({
        //   title: res.errMsg,
        //   icon: 'error'
        // })
      },

    })

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  add_click() {
    let arr = [];
    this.data.itemArr.map(val => {
      arr.push({
        goodsNum: val.goodsNum,
        goodsSid: val.goodsSid
      })
    })
    let url = "/v1/orders/goods-create";

    this.request({
      url: this.data.host + (this.data.sid ? "/v1/orders/member-create" : "/v1/orders/goods-create"),
      data: {
        goodsInfoVOS: arr,
        storeSid: this.data.storeSid,
        userSid: this.data.sid,
      },
      method: "post",
      loadingTip: "下单中..."
    }, res => {
      wx.setStorageSync('order', res.data)
      this.req_pay(res.data.goodsOrderSn)
    })
  },
  req_pay(sn) {
    this.setData({
      itemArr: []
    })
    wx.navigateTo({
      url: '/pages/store/settle/index?goodsOrderSn=' + sn + "&storeSid=" + this.data.storeSid + "&userSid=" + this.data.sid,
    })

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})